﻿_menu = function()
{
   this.Element = $get("Menu");
   //this.MenuContent = $get("MenuContent");
   this.Widget = {};
   this.Widget.Content = null;
   this.Widget.Loaded = false;
   this.RSS = {};
   this.RSS.Content = null;
   this.RSS.Loaded = false;
   this.Layout = {};
   this.Layout.Content = null;
   this.Layout.Loaded = false;
   this.Theme = {};
   this.Theme.Content = null;
   this.Theme.Loaded = false;
   this.Profile = {};
   this.Profile.Content = null;
   this.Profile.Loaded = false;
   this.LastItem = 1;
   this.Loaded = false;
   this.IsVisible = false;
}

_menu.prototype =
{
    Reset: function() {
        this.Element = $get("Menu");
        //this.MenuContent = $get("MenuContent");
        this.Widget = {};
        this.Widget.Content = null;
        this.Widget.Loaded = false;
        this.RSS = {};
        this.RSS.Content = null;
        this.RSS.Loaded = false;
        this.Layout = {};
        this.Layout.Content = null;
        this.Layout.Loaded = false;
        this.Theme = {};
        this.Theme.Content = null;
        this.Theme.Loaded = false;
        this.Profile = {};
        this.Profile.Content = null;
        this.Profile.Loaded = false;
        this.LastItem = 1;
        this.Loaded = false;
        this.IsVisible = false;
    },

    Toggle: function() {
        if (this.IsVisible) {
            this.Hide();
        }
        else {
            this.Show();
        }
    },

    Show: function(Item) {
        if (Item) {
            this.LastItem = Item;
        }
        this.Element.style.display = "";
        if (this.Loaded) {
            this.Show2();
        }
        else {
            setElementToAjaxLoading(this.Element);
            grabContent("/Menu/Menu.aspx", Function.createDelegate(this, this.ShowMenu));
        }
    },

    ShowMenu: function(content) {
        this.Element.innerHTML = content;
        this.Widget.Content = $get("MenuContentWidget");
        this.Profile.Content = $get("MenuContentProfile");
        this.Layout.Content = $get("MenuContentLayout");
        this.Theme.Content = $get("MenuContentTheme");
        this.RSS.Content = $get("MenuContentRSS");
        this.MenuNoPermission = $get("MenuNoPermission");
        this.Loaded = true;
        this.Show2();
    },

    Show2: function() {
        this.IsVisible = true;
        switch (this.LastItem) {
            case 2:
                this.ShowLayout();
                break;
            case 3:
                this.ShowTheme();
                break;
            case 4:
                this.ShowRSS();
                break;
            case 5:
                this.ShowProfile();
                break;
            default:
                this.ShowWidgets();
                break;
        }
    },

    Hide: function() {
        this.IsVisible = false;
        if (this.Element) {
            this.Element.style.display = "none";
        }
    },

    ShowContent: function(Item) {
        this.Widget.Content.style.display = "none";
        this.Layout.Content.style.display = "none";
        this.Theme.Content.style.display = "none";
        this.RSS.Content.style.display = "none";
        this.Profile.Content.style.display = "none";
        this.MenuNoPermission.style.display = "none";
        var Tab = myTabs.GetCurrentTab();

        switch (Item) {
            case 2:
                MenuItem_Click($('mLayout'), 'MenuItem');
                $('mLayout').className = 'MenuItem MenuItemOver';
                if (myTabs.hasRequiredTabPermission(Tab, TabAccessLevel.Tab_ChangeLayout)) {
                    this.Layout.Content.style.display = "";
                }
                else {
                    this.MenuNoPermission.style.display = "";
                }
                break;
            case 3:
                MenuItem_Click($('mTheme'), 'MenuItem');
                $('mTheme').className = 'MenuItem MenuItemOver';
                if (myTabs.hasRequiredTabPermission(Tab, TabAccessLevel.Tab_ChangeTheme)) {
                    this.Theme.Content.style.display = "";
                }
                else {
                    this.MenuNoPermission.style.display = "";
                }
                break;
            case 4:
                MenuItem_Click($('mRSS'), 'MenuItem');
                $('mRSS').className = 'MenuItem MenuItemOver';
                if (myTabs.hasRequiredTabPermission(Tab, TabAccessLevel.Tab_AddWidgets)) {
                    this.RSS.Content.style.display = "";
                }
                else {
                    this.MenuNoPermission.style.display = "";
                }
                break;
            case 5:
                MenuItem_Click($('mProfile'), 'MenuItem');
                $('mProfile').className = 'MenuItem MenuItemOver';
                this.Profile.Content.style.display = "";
                break;
            default:
                MenuItem_Click($('mWidget'), 'MenuItem');
                $('mWidget').className = 'MenuItem MenuItemOver';
                if (myTabs.hasRequiredTabPermission(Tab, TabAccessLevel.Tab_AddWidgets)) {
                    this.Widget.Content.style.display = "";
                }
                else {
                    this.MenuNoPermission.style.display = "";
                }
                break;
        }
    },

    ShowWidgets: function() {
        WriteHistory("ShowWidgets");
        this.ShowContent(1);
        if (!this.Widget.Loaded) {
            this.Element.style.display = "";
            grabContent("/Menu/Widgets.aspx", Function.createDelegate(this, this.DisplayWidgets));
        }
    },

    DisplayWidgets: function(content) {
        this.Widget.Content.innerHTML = content;
        removeChildNodes($get("MenuWidgetC1"));
        setElementToAjaxLoading($get("MenuWidgetC1"));
        removeChildNodes($get("MenuWidgets"));
        removeChildNodes($get("MenuWidgetInfo"));
        JSON.Menu_GetCategory(0, Function.createDelegate(this, this.DisplayWidgetsCategory));
    },

    DisplayWidgetsCategory: function(obj) {
        var Element = $get("MenuWidgetC1");
        removeChildNodes(Element);

        for (var i = 0; i < obj.length; i++) {
            var e = document.createElement("div");
            e.id = "MenuWidgetC1_" + obj[i].ID;
            e.appendChild(document.createTextNode(obj[i].Category));
            e.className = "MenuCategoryItem";
            e.onmouseover = function() { this.className = 'MenuCategoryItem MenuCategoryItemOver'; };
            e.onmouseout = function() { MenuItem_OnMouseOut(this, 'MenuCategoryItem'); };
            Element.appendChild(e);
            if (i == 0) {
                e.className += " MenuCategoryItemOver";
                Menu.DisplayWidgetsInCategory(obj[i].ID);
            }
            installScript("$get('" + e.id + "').onclick = function(){MenuItem_Click(this, 'MenuCategoryItem');Menu.DisplayWidgetsInCategory(" + obj[i].ID + ")};");
        }
    },

    DisplayWidgetsInCategory: function(id) {
        var Element = $get("MenuWidgets");
        removeChildNodes(Element);
        setElementToAjaxLoading(Element);
        JSON.Menu_GetCategoryWidgets(id, Function.createDelegate(this, this.DisplayWidgetsInCategory2));
    },

    DisplayWidgetsInCategory2: function(obj) {
        var Element = $get("MenuWidgets");
        removeChildNodes(Element);

        for (var i = 0; i < obj.length; i++) {
            var e = document.createElement("div");
            e.id = "MenuWidgets_" + obj[i].ID;
            e.onmouseover = function() { this.className = 'WidgetItem WidgetItemOver'; };
            e.onmouseout = function() { var cls = this.getAttribute("classx"); if (cls == null) cls = ""; this.className = (cls.length > 0 ? cls : "WidgetItem"); };
            var txt = "<table border='0'><tr valign='top'><td>";
            if (obj[i].Image) {
                txt += "<img src='" + obj[i].Image + "' border='0' /></td><td>";
            }
            txt += obj[i].Title + "</td></tr></table>";
            e.innerHTML = txt;
            Element.appendChild(e);
            if (i == 0) {
                e.className += " WidgetItemOver";
                this.DisplayWidgetInfo(obj[i].ID);
            }
            installScript("$get('" + e.id + "').onclick = function(){MenuItem_Click(this, 'WidgetItem');Menu.DisplayWidgetInfo(" + obj[i].ID + ");}");
        }
    },

    DisplayWidgetInfo: function(id) {
        var Element = $get("MenuWidgetInfo");
        removeChildNodes(Element);
        setElementToAjaxLoading(Element);
        JSON.Menu_GetWidgetInfo(id, Function.createDelegate(this, this.DisplayWidgetInfo2));
    },

    DisplayWidgetInfo2: function(obj) {
        var Element = $get("MenuWidgetInfo");
        removeChildNodes(Element);

        if (obj) {
            var txt = "<table border='0'><tr valign='top'><td>";
            if (obj.Image) {
                txt += "<img src='" + obj.Image + "' border='0' /></td><td>";
            }
            txt += obj.Title + "</td></tr></table>";
            txt += "<div>" + obj.Description + "</div>";
            var TabTitle = myTabs.GetCurrentTab().obj.Title;
            if (TabTitle) {
                if (TabTitle.length > 10) {
                    TabTitle = TabTitle.substring(0, 8) + "..";
                }

                TabTitle = "&quot;" + TabTitle + "&quot; Tab";
            }
            else {
                TabTitle = "Tab";
            }
            txt += "<input type='button' id='MenuWidgetAddWidgetBtn' value='Add To " + TabTitle + "' class='AddWidget' onclick='myWidgets.AddWidget(" + obj.ID + ")' />";
            /*         
            obj.ID
            obj.URL
            obj.DefaultState
            */

            Element.innerHTML = txt;
        }
        this.Widget.Loaded = true;
    },

    ShowRSS: function() {
        WriteHistory("ShowRSS");
        this.ShowContent(4)
        if (!this.RSS.Loaded) {
            grabContent("/Menu/RSS.aspx", Function.createDelegate(this, this.DisplayRSS));
        }
    },

    DisplayRSS: function(content) {
        this.RSS.Content.innerHTML = content;
        setDefaultButton("RSS_XmlFile", "btn_RSS");
        $get('RSS_XmlFile').focus();
        this.RSS.Loaded = true;
    },

    ShowProfile: function() {
        WriteHistory("ShowProfile");

        this.ShowContent(5)

        if (!this.Profile.Loaded) {
            grabContent("/Menu/Profile.aspx", Function.createDelegate(this, this.DisplayProfile));
        }
    },

    DisplayProfile: function(content) {
        this.Profile.Content.innerHTML = content;
        setDefaultButton("mp_Name", "btn_Profile");
        this.Profile.Loaded = true;
        $get("mp_Name").focus();
    },

    UpdateProfile: function() {
        WriteHistory("Update Profile");
        var FullName = $get("mp_Name").value;

        if (FullName.length < 1) {
            alert("Please enter a UserName");
            $get("mp_Name").focus();
        }
        else {
            $show('mp_progress');
            JSON.User_UpdateProfile(FullName, Function.createDelegate(this, this.UpdateProfileComplete));
            this.Hide();
        }
    },

    UpdateProfileComplete: function(fullname) {
        $hide("mp_progress");
        if (fullname.length < 1) {
            alert("Invalid return value");
        }
        else {
            $get('head_fullname').innerHTML = fullname;
        }
    },

    ShowLayout: function() {
        WriteHistory("ShowLayout");

        this.ShowContent(2)
        if (!this.Layout.Loaded) {
            grabContent("/Menu/Layout.aspx", Function.createDelegate(this, this.DisplayLayout));
        }
    },

    DisplayLayout: function(content) {
        this.Layout.Content.innerHTML = content;
        //Get the current layout
        var Tab = myTabs.GetCurrentTab();
        if (Tab.obj.InnerHTML && Tab.obj.InnerHTML.length > 0) {
            document.getElementById("layout_innerhtml").value = Tab.obj.InnerHTML;
        }
        else {
            var RB = "rb_" + Tab.obj.PanelWidth_1 + "_" + Tab.obj.PanelWidth_2 + "_" + Tab.obj.PanelWidth_3;
            $get(RB).checked = true;
        }
        this.Layout.Loaded = true;
    },


    ShowThemes: function() {
        WriteHistory("ShowThemes");

        this.ShowContent(3);
        if (!this.Theme.Loaded) {
            setElementToAjaxLoading(this.Theme.Content);
            JSON.Menu_GetThemes(Function.createDelegate(this, this.DisplayThemes));
        }
    },

    DisplayThemes: function(obj) {
        if (obj && obj.length > 0) {
            removeChildNodes(this.Theme.Content);

            for (var i = 0; i < obj.length; i++) {
                var Item = obj[i];
                var e = document.createElement("div");
                e.id = "MenuTheme_" + i;
                e.appendChild(document.createTextNode(Item));
                this.Theme.Content.appendChild(e);
                installScript("$get('" + e.id + "').onclick = function(){Menu.ChangeTheme('" + Item + "');}");
            }
            this.Theme.Loaded = false;
        }
        else {

            this.Theme.Content = "We are still working on the Themes Menu, please bear with us";
        }
    },

    ChangeTheme: function(theme) {
        var AllLinks = document.getElementsByTagName("link");

        for (var i = 0; i < AllLinks.length; i++) {
            var a = AllLinks[i];
            if (a.getAttribute("rel").indexOf("style") != -1) {
                //this is a css link
                var href = a.getAttribute("href");
                var x = href.toLowerCase();
                var stringToFind = "app_themes/";
                var idx = x.toLowerCase().indexOf(stringToFind);
                if (idx > -1) {
                    idx = idx + stringToFind.length;
                    var idx2 = x.indexOf("/", idx);
                    if (idx2 > -1) {
                        a.href = x.substring(0, idx) + theme + x.substring(idx2);
                    }
                }
                else {
                    //not part of theme css, ignore
                }
            }
        }
    }
}